using System;
using System.Collections.Generic;
using ProtoModels;
using UnityEngine;

public class SeasonDefinitions : ScriptableObject
{
	public enum SeasonId
	{
		Season1 = 0,
		Season2 = 1
	}

	[Serializable]
	public class Season
	{
		public SeasonId seasonId;

		public string seasonName;

		public List<SeasonInterval> seasonIntervals = new List<SeasonInterval>();

		public SeasonInterval GetNextInterval(SeasonInterval currentInterval)
		{
			int num = -1;
			bool flag = false;
			foreach (SeasonInterval seasonInterval in seasonIntervals)
			{
				num++;
				if (seasonInterval == currentInterval)
				{
					flag = true;
					break;
				}
			}
			int num2 = num + 1;
			if (flag && num2 < seasonIntervals.Count)
			{
				return seasonIntervals[num2];
			}
			return null;
		}
	}

	public enum SeasonIntervalId
	{
		Interval1 = 0,
		Interval2 = 1,
		Interval3 = 2,
		Interval4 = 3,
		Interval5 = 4,
		Interval6 = 5,
		Interval7 = 6,
		Interval8 = 7,
		Interval9 = 8,
		Interval10 = 9,
		Interval11 = 10,
		Interval12 = 11,
		Interval13 = 12
	}

	[Serializable]
	public class SeasonInterval
	{
		public SeasonIntervalId intervalId;

		public string intervalName;

		public List<SeasonEvent> eventsForInterval = new List<SeasonEvent>();

		public bool isPlayedAllEvents
		{
			get
			{
				TournamentCareerStats careerStatsManager = GGPlayerSettings.instance.careerStatsManager;
				foreach (SeasonEvent item in eventsForInterval)
				{
					TournamentCareerDAO.ActiveSeasonInterval.ActiveEvent activeEvent = careerStatsManager.GetActiveEvent(item.namedEventId);
					if (!activeEvent.isStarted || !activeEvent.isFinished)
					{
						return false;
					}
				}
				return true;
			}
		}

		public bool isCurrentInterval
		{
			get
			{
				TournamentCareerStats careerStatsManager = GGPlayerSettings.instance.careerStatsManager;
				SeasonIntervalId activeIntervalId = careerStatsManager.GetActiveIntervalId();
				return activeIntervalId == intervalId;
			}
		}

		public bool isBeforeCurrentInterval
		{
			get
			{
				return !isCurrentInterval && !isAfterCurrentInterval;
			}
		}

		public bool isAfterCurrentInterval
		{
			get
			{
				TournamentCareerStats careerStatsManager = GGPlayerSettings.instance.careerStatsManager;
				SeasonIntervalId activeIntervalId = careerStatsManager.GetActiveIntervalId();
				return activeIntervalId < intervalId;
			}
		}
	}

	public enum NamedEventId
	{
		IrelandOpen = 0,
		GermanyOpen = 1
	}

	[Serializable]
	public class SeasonEvent
	{
		public NamedEventId namedEventId;

		public string eventName;

		public GameConstants.Flags country;

		public int pointsToWinGame = 11;

		public int gamesToWinMatch = 1;

		public Events.EventDefinitionId eventDefinitionId;

		public Events.EventDefinition GetEvent()
		{
			return EventsDB.instance.GetEventById(eventDefinitionId);
		}
	}

	public struct NextIntervalInfo
	{
		public Season season;

		public SeasonInterval seasonInterval;
	}

	private static SeasonDefinitions instance_;

	public List<Season> seasons = new List<Season>();

	public static SeasonDefinitions instance
	{
		get
		{
			if (instance_ == null)
			{
				instance_ = Resources.Load("TournamentsDefinitions/SeasonDefinitions", typeof(SeasonDefinitions)) as SeasonDefinitions;
			}
			return instance_;
		}
	}

	public SeasonInterval GetIntervalForIds(SeasonId seasonId, SeasonIntervalId intervalId)
	{
		Season season = GetSeason(seasonId);
		if (season == null)
		{
			return null;
		}
		int intervalIndex = GetIntervalIndex(season, intervalId);
		return (intervalIndex < 0) ? null : season.seasonIntervals[intervalIndex];
	}

	public Season GetSeason(SeasonId seasonId)
	{
		int seasonIndex = GetSeasonIndex(seasonId);
		return (seasonIndex < 0) ? null : seasons[seasonIndex];
	}

	public NextIntervalInfo GetInfoForNextInterval(SeasonId currentSeasonId, SeasonIntervalId currentIntervalId)
	{
		int seasonIndex = GetSeasonIndex(currentSeasonId);
		Season season = seasons[seasonIndex];
		int intervalIndex = GetIntervalIndex(season, currentIntervalId);
		int index = ((intervalIndex < season.seasonIntervals.Count - 1) ? (intervalIndex + 1) : 0);
		int num = ((intervalIndex >= season.seasonIntervals.Count - 1) ? (seasonIndex + 1) : seasonIndex);
		if (num >= seasons.Count)
		{
			num = 0;
		}
		NextIntervalInfo result = default(NextIntervalInfo);
		result.season = seasons[num];
		result.seasonInterval = result.season.seasonIntervals[index];
		return result;
	}

	private int GetSeasonIndex(SeasonId seasonId)
	{
		for (int i = 0; i < seasons.Count; i++)
		{
			if (seasons[i].seasonId == seasonId)
			{
				return i;
			}
		}
		return -1;
	}

	private int GetIntervalIndex(Season season, SeasonIntervalId intervalId)
	{
		for (int i = 0; i < season.seasonIntervals.Count; i++)
		{
			if (season.seasonIntervals[i].intervalId == intervalId)
			{
				return i;
			}
		}
		return -1;
	}
}
